/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_DRC_DRC_CORE_H_
#define WEBRTC_MODULES_AUDIO_PROCESSING_DRC_DRC_CORE_H_

#include <stdint.h>

#define BLOCKL_MAX          160 // max processing block length: 160
#define DRC_BLOCKL_MAX     256 // max analysis block length: 256
#define HALF_DRC_BLOCKL    129 // half max analysis block length + 1
#define NUM_HIGH_BANDS_MAX  2   // max number of high bands: 2

#define QUANTILE            (float)0.25

#define SIMULT              3
#define END_STARTUP_LONG    200
#define END_STARTUP_SHORT   50
#define FACTOR              (float)40.0
#define WIDTH               (float)0.01

// Length of fft work arrays.
#define IP_LENGTH (DRC_BLOCKL_MAX >> 1) // must be at least ceil(2 + sqrt(DRC_BLOCKL_MAX/2))
#define W_LENGTH (DRC_BLOCKL_MAX >> 1)

#define DRC_WORD16_MAX       32767
#define DRC_WORD16_MIN       -32768

#define DRC_SAT(a, b, c) (b > a ? a : b < c ? c : b) 

typedef struct DynamicRangeCompressionC_ {
  uint32_t fs;
  size_t blockLen;
  size_t windShift;
  size_t drcLen;
  size_t magnLen;
  const float* window;
  float dataBuf[DRC_BLOCKL_MAX];
  float syntBuf[DRC_BLOCKL_MAX];

  int initFlag;
  int threshold1;
  float ratio;
  int threshold2;
  int mode;
  
  float supPoint;
  float supExp;
    
  size_t ip[IP_LENGTH];
  float wfft[W_LENGTH];

} DynamicRangeCompressionC;

#ifdef __cplusplus
extern "C" {
#endif

/****************************************************************************
 * Drc_InitCore(...)
 *
 * This function initializes a dynamic range compression instance
 *
 * Input:
 *      - self          : Instance that should be initialized
 *      - fs            : Sampling frequency
 *
 * Output:
 *      - self          : Initialized instance
 *
 * Return value         :  0 - Ok
 *                        -1 - Error
 */
int Drc_InitCore(DynamicRangeCompressionC* self, uint32_t fs, int threshold1, float ratio, int threshold2, int mode, float supPoint, float supExp);

/****************************************************************************
 * Drc_ProcessCore
 *
 * Do dynamic range compression.
 *
 * Input:
 *      - self          : Instance that should be initialized
 *      - inFrame       : Input speech frame for each band
 *      - num_bands     : Number of bands
 *
 * Output:
 *      - self          : Updated instance
 *      - outFrame      : Output speech frame for each band
 */
void Drc_ProcessCore(DynamicRangeCompressionC* self,
                          const float* const* inFrame,
                          float* const* outFrame);

#ifdef __cplusplus
}
#endif
#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_DRC_DRC_CORE_H_
